package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118625716";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCfVlWXY3KZSLqH1GZaYHAG4Pbu9IaAv8DtD8gnz/ruyyxQk3jre2DCHcpm/90oTHO4C+ZTjlDWk8g2fDryCEbibV3rYn776mpLEuCLEZz738V3hnvOLGuqNgmpJP80EtI+tWq5aM0ZGpPiYbEDx++m6NM6HybAfhSUUQyOlDewXPgO4S10kwkEcQgqAy1On9+GD64QyrdhvvOqU0sKLB6z/PwS7dcq2dymmnK+axPPSOQZfqFKSLP7Eu5XNHord9nsRdfa9fAoRFPKoZGHE4+0qqcMA6X/4/o1LlWjkd4eartHubjccPUi2G8Z38zgfEnXIYab7OHwK+cR/PiQxfh5AgMBAAECggEBAI/aQiwLfMsm/kDjYgT2TC3prQbPfR9//44HUY2gF8y/YaLdga4YFaMrgjC7j89yNodTNv743cLcaUXHovAgyN5kHwcEReDqIkzlHhRwMmqz/HOM1vms4QcrAk3TORSqaWZ0I4aBN6oSdLFNwaLIP3VRfmJa/YGMUsgU5GFVL3HpDwTvaYxKCL75b2NZi1gKJAuwe5d59nSnGAPYRYyPR4GIS1+jNk/ndHv5l6KTe97Nw7cnSO6PjnHAJwD1h42LR82i6e00TizvZ/c56hWF9AC7wM5wXuPAQVO2B2vhkW3ZEDzIVuZX/Oj5/Pa7ePANWdn8ubD3KsPBej2XV53YWgECgYEA30/Rkx6PPTCZgkrvlRaj3h11GZ+UAWAsLakvoSYChdycp+ooZIPm5SNvvUm60C75dDpGSeorwgSjrrjxEWYEkkfHpaypZ80w/b2eZx2xQZD65QDbgDtuBu8cj4ykN8pJHai0JNSq4meH63OWadoBcBbB2CjxlJrtvF3HIKokcl0CgYEAtqkxLzEZ/wQnEkbgt/yiKpK+RO+HOv8XfsrLkRZUtSbHsozPUk9RX3nUXhvA4qE835UZmrUn5VuUqYe1340Es6qtTn54lKw4NnRtxNvQv+tElqnjasS9yRj4mKssPgG0zL08mPN+wXSasxERG2UnmOxxUXzgmV2isUjVP+0RtM0CgYEAn0o0WBkIW8F6/rGtTE+l/B5PFUP7MzAC019Hlk+ipRHEvRIOVgSwgupQHlmlZDLdxcZ7tOXQ6MieaVeKvbpjsGR7SPZssJpxQ84qW6uWcQqVrRx7dnDt/ww49zx/NzyYAwv/0/XPH0hyb8U3mbHkLKxPAX3TNQl+Y0sRa39yGBECgYEAkv22kdZ+qASseH3HKuhHi+EI8qqa9XQAcU064efJhPnPLDU6RW+E+UMuDglhgD7moLn4n9H2aBD5oi+OwXZW+Up5KriFyoaIufGZwBhwdXEbge73Dp2CaIkKzLi64q820qfaUu9n6sAuGbm7PYN6iQqE6mGG0IRUfzwwN8BMRmUCgYEAlI2hyu24r9C4JcWFbQYgWnmF0pwMpsnLu9JKbh4wkZahDi8XKZN2aJskbTRZCre73/O/biuxF+6AQEei1qZd0/Ds/8Dh8BKt033xcFER4clto4jwcV+ZRVflkDEJy+dJ3+uu4TPu3OKFEfITkB9dllvkFrc2pImJxzJDXIZIhi0=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtjbbDyZ3/OIouxKJnl6KbcNyEEP0U05w2/ncImjZSMSRpCS6E3JAJm7Y0AvYoTru3rpcC3aR4bves0fTcXVmvY55LqdoGvc70OzQL0R6Z3h3iBTMKnJzgCO6semCSrba9CHENWmSytspM6wIBzkqThqnPPxUG3BpTYik2KxhBhzepubPqFIFwZBLGfn15FSGjt1jijzAHXONoZrHHlZ4MT/5xpOy9CIbb5EfOaGZJBB0wSlclQiVmHIN9Yqtw5SKk8Uv5rQCE2PgbscAap823sCPYk7Q4lRP07tLYWbYENhH3tCcGB1j33n2Ox+i+iJVuUFR2pLYKVtTegq8CmTWlQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://5dz8mi.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "15m";
    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
